import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: Conn
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/16 17:04
 */
public class Conn {
    @Test
    @DisplayName("连接数据库")
    void conn1() throws ClassNotFoundException, SQLException {
        //手动加载jdbc驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
        //
        var con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "admin");
        //prepareStatement预编译
        var sta = con.prepareStatement("select * from t1");
        var rs = sta.executeQuery();
        while (rs.next()) {
            System.out.print(" " + rs.getString(1));
            System.out.print(" " + rs.getString(2));
            System.out.print(" " + rs.getString(3));
            System.out.print(" " + rs.getString(4));
            System.out.print(" " + rs.getString(5));
            System.out.println(" " + rs.getString(6));
        }
        System.out.println(con.getMetaData().getDatabaseProductVersion());
    }
}
